package com.app.model 
{
	import flash.utils.Dictionary;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	import org.puremvc.as3.interfaces.IProxy;
	
	import com.app.model.xml.XmlManager;
	
	import com.app.model.data.UserData;
	import com.app.model.data.LevelData;
	
	import com.peachcastle.debug.Log;
	import com.peachcastle.debug.LogArray;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class GameConfigProxy extends Proxy implements IProxy
	{
		public static const NAME:String = 'GameConfigProxy';
		
		public static const BANKER_SEATID:int = 1;
		
		/**
		 * 自己
		 */
		private var _self:UserData = null;
		private var _mtkey:String = '';
		
		/**
		 * 全局
		 */
		private var _xmlManager:XmlManager = null;
		
		private var _webs:Object = null;
		
		private var _hallIp:String = '';
		private var _hallPort:int = 0;
		
		private var _roomIp:String = '';
		private var _roomPort:int = 0;
		private var _serverid:int = 0;
		private var _roomid:int = 0;
		private var _isFastSit:Boolean = false;
		private var _isReExchangeChip:Boolean = false;
		private var _requestSeatid:int = 0;
		
		/// 舞台尺寸
		private var _stageWidth		:int = 760;		// 舞台宽度
		private var _stageHeight	:int = 770;		// 舞台高度	
		
		public function GameConfigProxy() 
		{
			super(NAME);
		}
		
		override public function onRegister():void
		{
			_xmlManager = new XmlManager();
		}
		
		public function setStageSize(w:int, h:int):void
		{
			_stageWidth = w;
			_stageHeight = h;
		}

		public function parseUserFromWeb(obj:Object = null):void
		{
			if (!obj) 
			{
				throw("config not user data parse...");
				return;
			}
			
			var data:UserData = new UserData();
			
			switch(Log.console) 
			{
				case true:
					data.mid = obj['uid'];
					data.name = 'kevin';
					data.sex = 1;
					data.chips = 10000;
					data.exp = 10;
					data.star = 1;
					data.sublevel = 1;
					data.icon = 'http://hd24.xiaonei.com/photos/hd24/20080119/23/12/tiny_9094o169.jpg';
					
					data.star = 1;
					data.sublevel = 1;
					data.isAnonymous = false;
					
					
					break;
			
				case false:
					//cfg.flash_vars.user
					data.mid = obj["user"]["mid"];
					data.name = obj["user"]["mnickname"];
					data.sex = obj["user"]["sex"];
					data.icon = obj["user"]["pic"];
					data.middle = '';
					data.big = '';
					data.chips = obj["user"]["chips"];
					data.exp = obj["user"]["exp"];
					data.star = obj["user"]["star"];
					data.sublevel = obj["user"]["sublevel"];
					
					data.isAnonymous = obj["is_anonymous"] == '1' ? true : false;
					
					_serverid = obj["user"]["sid"];
					_roomid = obj["user"]["rid"];
					
					break;
			}
			
			_self = data;
		}
		
		public function parseConfigFromWeb(obj:Object = null):void
		{
			_hallIp = obj['host'];
			_hallPort = obj['port'];
			if (!hallIp || !hallPort)
			{
				throw("hall ip or port is empty!");
			}
			
			Log.enabled = true;
			switch(Log.console)
			{
				case true:
					_mtkey = obj['mtkey'];
					
					Log.level = 'debug';
					
					break;
			
				case false:
					_mtkey = obj["user"]["mtkey"];
					
					Log.level = 'debug';
					if (obj.hasOwnProperty("flash_log"))
					{
						Log.level = obj['flash_log'];
					}
					
					break;
			}
			
			
			_webs = obj;
		}
		
		public function initLog():void
		{
			LogArray.initLog();
		}
		
		/**
		 * 获取swf的地址
		 * @param	swfName
		 * @return
		 */
		public function getSwfUrl(swfName:String):String
		{	
			if (Log.console)
			{
				return swfName + ".swf";
			}
			
			var swf:String = "";
			if (typeof webs["vers"] != "undefined")
			{
				swf = webs["vers"][swfName];
			}
			
			var url:String = webs["flash_path"];
			return 	(url + swf);
			
		}
		
		public function getDialogUrl(swfName:String):String
		{
			if (Log.console)
			{
				return "dialogs/" + swfName + ".swf";
			}
			
			var swf:String = "";
			if (typeof webs["vers"] != "undefined")
			{
				swf = webs["vers"][swfName];
			}
			
			var url:String = webs["flash_path"];
			return 	(url + "dialogs/" + swf);
		}	
		
		public function getbgSoundUrl(soundName:String):String
		{
			if (Log.console)
			{
				return "mp3/" + soundName + ".mp3";
			}
			
			var sound:String = soundName + '.mp3';
			var url:String = webs["flash_path"];
			return 	(url + "mp3/" + sound);
		}
		
		public function getXmlUrl(xmlName:String):String
		{	
			if (Log.console)
			{
				return 'xml/' + xmlName + ".data";
			}
			
			var xml:String = "";
			if (typeof webs["xml_path"] != "undefined")
			{
				xml = webs["xml_path"] + xmlName + ".data?v=" + webs["xml_ver"];
			}
			
			return xml;
			
		}
		
		public function getCurrentLevelExp(currentExp:int, exps:Dictionary):LevelData
		{
			var data:LevelData = null;
			for each(var d:LevelData in exps)
			{
				if (d.totalExp > currentExp)
				{
					data = d;

					break;
				}
			}
			
			return data;
		}

		public function get self():UserData { return _self; }
		public function get mtkey():String { return _mtkey; }
		
		public function get hallIp():String { return _hallIp; }
		public function get hallPort():int { return _hallPort; }
		public function set roomIp(ip:String):void { _roomIp = ip; }
		public function get roomIp():String { return _roomIp; }
		public function set roomPort(port:int):void { _roomPort = port; }
		public function get roomPort():int { return _roomPort; }
		public function set serverid(serverid:int):void { _serverid = serverid; }
		public function get serverid():int { return _serverid; }
		public function set roomid(roomid:int):void { _roomid = roomid; }
		public function get roomid():int { return _roomid; }
		public function set isFastSit(v:Boolean):void { _isFastSit = v; }
		public function get isFastSit():Boolean { return _isFastSit; }
		public function set isReExchangeChip(v:Boolean):void { _isReExchangeChip = v; }
		public function get isReExchangeChip():Boolean { return _isReExchangeChip; }
		public function set requestSeatid(v:int):void { _requestSeatid = v; }
		public function get requestSeatid():int { return _requestSeatid; }
		public function get webs():Object { return _webs; }
		
		public function get xmlManager():XmlManager { return _xmlManager; }
		
		public function get stageWidth():int { return _stageWidth; }
		public function get stageHeight():int { return _stageHeight; }
		
		
		/***
		 * 游戏逻辑的全局变量
		 * 
		 */ 
		private var _gameStatus:int = 0;
		public function get gameStatus():int { return _gameStatus; }
		public function set gameStatus(gameStatus:int):void { _gameStatus = gameStatus; }
	}
}